/**
 * Copyright(c) kyle.
 */
package org.example.jvm.remote_executor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author kyle
 * @version 1.00.00
 * @since [2020-12-05 16:24]
 * @updateUser [Kyle]
 * @updateRemark [说明本次修改内容]
 * @date [2020-12-05 16:24]
 */
public class JavaclassExecuter {
    public static String execute(byte[] classByte) {
        HackSystem.clearBuffer();
        ClassModifier cm = new ClassModifier(classByte);
        byte[] modiBytes = cm.modifyUTF8Constant("java/lang/System", "org/example/jvm/remote_executor/HackSystem");
        HotSwapClassLoader loader = new HotSwapClassLoader();
        Class<?> clazz = loader.loadByte(modiBytes);
        try {
            Method method = clazz.getMethod("main", String.class);
            method.invoke(null, "干的漂亮");
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
            System.out.println("getMethod出错");
        } catch (IllegalAccessException | IllegalArgumentException |
                InvocationTargetException e) {
            System.out.println("invoke出错");
        }
        return HackSystem.getBufferString();
    }
}
